package com.opencloud.admin.controller;

import com.opencloud.admin.service.feign.SystemAuthorityServiceClient;
import com.opencloud.admin.service.feign.SystemUserServiceClient;
import com.opencloud.common.constants.CommonConstants;
import com.opencloud.common.model.Result;
import com.opencloud.common.security.XSecurityHelper;
import com.opencloud.common.security.XUserDetails;
import com.opencloud.common.utils.AesUtil;
import com.opencloud.system.client.model.AuthorityMenu;
import com.opencloud.system.client.model.entity.SystemUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: liuyadu
 * @date: 2019/5/24 13:31
 * @description:
 */
@Api(tags = "当前登陆用户")
@RestController
public class CurrentUserController {

    @Autowired
    private SystemUserServiceClient systemUserServiceClient;
    @Autowired
    private SystemAuthorityServiceClient systemAuthorityServiceClient;
    @Autowired
    private RedisTokenStore redisTokenStore;
    /**
     * 修改当前登录用户密码
     *
     * @return
     */
    @ApiOperation(value = "修改当前登录用户密码", notes = "修改当前登录用户密码")
    @GetMapping("/current/user/rest/password")
    public Result restPassword(@RequestParam(value = "password") String password) {
        AesUtil aesUtil = new AesUtil();
        password = aesUtil.decrypt("d1d909d038bf481e9479f66677e0aad0",
                "964f99fdb425af9a91f401750bc675fa",
                "I5OCkMBFw4VbeUSDfTgfPyws",
                password);
        systemUserServiceClient.updatePassword(XSecurityHelper.getUser().getUserId(), password);
        return Result.ok();
    }

    /**
     * 修改当前登录用户基本信息
     *
     * @param nickName
     * @param userDesc
     * @param avatar
     * @return
     */
    @ApiOperation(value = "修改当前登录用户基本信息", notes = "修改当前登录用户基本信息")
    @PostMapping("/current/user/update")
    public Result updateUserInfo(
            @RequestParam(value = "nickName") String nickName,
            @RequestParam(value = "userDesc", required = false) String userDesc,
            @RequestParam(value = "avatar", required = false) String avatar
    ) {
        XUserDetails xUserDetails = XSecurityHelper.getUser();
        SystemUser user = new SystemUser();
        user.setUserId(xUserDetails.getUserId());
        user.setNickName(nickName);
        user.setUserDesc(userDesc);
        user.setAvatar(avatar);
        systemUserServiceClient.save(user);
        xUserDetails.setNickName(nickName);
        xUserDetails.setAvatar(avatar);
        XSecurityHelper.updateUser(redisTokenStore, xUserDetails);
        return Result.ok();
    }

    /**
     * 获取登陆用户已分配权限
     *
     * @return
     */
    @ApiOperation(value = "获取当前登录用户已分配菜单权限", notes = "获取当前登录用户已分配菜单权限")
    @GetMapping("/current/user/menu")
    public Result<List<AuthorityMenu>> findAuthorityMenu() {
        return systemAuthorityServiceClient.findAuthorityMenuByUser(XSecurityHelper.getUser().getUserId(), CommonConstants.ROOT.equals(XSecurityHelper.getUser().getUsername()));
    }

    /**
     * 获取当前登录用户所进应用的菜单权限列表
     *
     * @param appId 应用ID
     * @return
     */
    @ApiOperation(value = "获取当前登录用户所进应用的菜单权限列表", notes = "获取当前登录用户所进应用的菜单权限列表")
    @GetMapping("/current/user/app/menu")
    public Result<List<AuthorityMenu>> findAuthorityMenuByApp(
            @RequestParam(value = "APP_ID") String appId) {
        return systemAuthorityServiceClient.findAuthorityAppMenuByUser(
                XSecurityHelper.getUserId(), appId);
    }

}
